// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/storeAll.js'
import util from './util'
import directive from './directive'


Vue.use(util);
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  data() {
    return {
      TIMEOUT: ""
    }
  },
  el: '#app',
  router,
  store,
  directive,
  template: '<App/>',
  components: {App},
  computed: {
    indexTitle() {
      return this.$store.state.indexTitle
    }
  },
  beforeCreate(){
    $(window).resize(() => {
      if (this.TIMEOUT) {
        clearTimeout(this.TIMEOUT);
      }
      this.TIMEOUT = setTimeout(() => {
        this.resizeWindow();
      }, 100);
    });

  },
  mounted() {
    this.resizeWindow();
    /**获取微信API权限
     *
     * @type {string}
     */
    let urlWithoutHash = "";
    if(location.hash == "") {
      urlWithoutHash = location.href;
    } else {
      urlWithoutHash = location.href.split('#')[0];
    }
    $.ajax({
      url: config.appCode + config.szg.obtainMessAge,
      type:"GET",
      data:{"uname": urlWithoutHash}
    }).done(function(data){
      let Json = {
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: data.result.appId, // 必填，公众号的唯一标识
        timestamp: data.result.timestamp, // 必填，生成签名的时间戳
        nonceStr: data.result.nonceStr, // 必填，生成签名的随机串
        signature: data.result.signature,// 必填，签名，见附录1
        jsApiList: ['chooseImage','previewImage','uploadImage','scanQRCode'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
      };
      wx.config(Json);
      wx.error(function(res){
        // alert(res);
        // alert(JSON.stringify(res));
        warning.init("微信接口错误");
        // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。

      })
    });
  },
  methods: {
    resizeWindow() {
      let windowW = $(window).width();
      let windowH = $(window).height();
      let maxWidth = $('body').css('maxWidth');
      console.log(maxWidth+'煞笔徐翔');
      if (maxWidth !== 'none') {
        maxWidth = parseInt(maxWidth);
      }
      if (windowW < maxWidth) {
        $('html,body').css('font-size', windowW * 12 / 320);
      } else {
        $('html,body').css('font-size', maxWidth * 12 / 320);
      }
      this.$store.commit('windowWidthMU', windowW);
      this.$store.commit('windowHeightMU', windowH);
    },
    checkWX() {
      console.log(config.isProduct);
      if (config.isProduct) {
        //有2种code 地址栏内和Storage内
        let code = DYD.getCode();
        if (!code) {
          window.location.href=config.szg.start;
        }
        let codeFromStorage=Storage.get('urlCode');
        if(code==codeFromStorage){
          return;
        }
        Storage.set('urlCode',code);
        //
        console.log(config.jc.info.openid());
          // if (code != "") {
          ajaxCore.ajax(config.jc.getOpenidByCode, {code: code}, "GET", null).done((data) => {
            if (data.status == 10001) {
              //未绑定
              config.jc.info.access_token = data.result.access_token;
              Storage.set("access_token",config.jc.info.access_token);
              Storage.set("openid", data.result.openid);
              DYD.msg("获取微信信息成功");
              this.$router.push('/login')
            } else if (data.status == 10101) {
              //已经绑定
              config.jc.info.Token = data.result.token;
              config.jc.info.baseToken = BASE64.encode("token:" + data.result.token);
              config.jc.info.nickName = data.result.nickname;
              config.jc.info.nickImg = data.result.imgurl;
              config.jc.info.jf = data.result.jf;
              config.jc.info.flag = data.result.flag;
              Storage.set("openid", data.result.openid);
              Storage.set("Token", config.jc.info.Token);
              Storage.set("baseToken", config.jc.info.baseToken);
              Storage.set("nickName", config.jc.info.nickName);
              Storage.set("nickImg", config.jc.info.nickImg);
              Storage.set("jf", config.jc.info.jf);
              Storage.set("flag", config.jc.info.flag);
              DYD.msg("登录成功");
              this.checkParamURL();
              if(this.$route.name=='login'){
                this.$router.push('/plain')
              }

            } else if (data.status == 10002) {
              //增加跳转回登录页面
              this.$router.push('/login')
              DYD.msg(data.desc)
            } else {
              DYD.msg("获取微信信息失败");
            }
          });
      }
      let urlWithoutHash = "";
      if(location.hash == "") {
        urlWithoutHash = location.href;
      } else {
        urlWithoutHash = location.href.split('#')[0];
      }
      $.ajax({
        url: config.appCode + config.szg.obtainMessAge,
        type:"GET",
        data:{"uname": urlWithoutHash}
      }).done(function(data){
        let Json = {
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: data.result.appId, // 必填，公众号的唯一标识
          timestamp: data.result.timestamp, // 必填，生成签名的时间戳
          nonceStr: data.result.nonceStr, // 必填，生成签名的随机串
          signature: data.result.signature,// 必填，签名，见附录1
          jsApiList: ['chooseImage','previewImage','uploadImage','scanQRCode'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        };
        wx.config(Json);
        wx.error(function(res){
          // alert(res);
          // alert(JSON.stringify(res));
          warning.init("微信接口错误");
          // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。

        })
      });
    },
    checkParamURL(){
        DYD.getTypeByUrl(window.location.href,1)
    }
  },
  watch: {
    $route(to, routerFrom) {
      this.checkWX();
    },
    indexTitle(to){
      // $('title').html(to);
    }
  }
});
